package com.xuexiang.xuidemo.slice.imageview.edit;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.bundle.IBundleManager;
import ohos.utils.net.Uri;

import com.xuexiang.xui_lib.component.button.SuperButton;
import com.xuexiang.xui_lib.component.imageview.crop.CropDrawableUtils;
import com.xuexiang.xui_lib.component.imageview.crop.CropImageType;
import com.xuexiang.xui_lib.component.imageview.crop.CropImageView;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;

public class ImageCropSlice extends BaseAbilitySlice implements Component.ClickedListener {
    private static final int MY_PERMISSIONS_REQUEST_STORE = 100;
    private SuperButton sBtnSelect;
    private SuperButton sBtnRotate;
    private SuperButton sBtnCrop;
    private CropImageView mCropImageView;
    private static final String[] storePermissions =
        {
            "ohos.permission.READ_USER_STORAGE",
            "ohos.permission.WRITE_USER_STORAGE"

        };
    /**
     * 选择系统图片Request Code
     */
    public static final int REQUEST_IMAGE = 112;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_image_crop_layout;
    }

    @Override
    protected void initComponents() {
        initView();
    }

    private void initView() {
        sBtnSelect = (SuperButton) findComponentById(ResourceTable.Id_btn_select);
        sBtnRotate = (SuperButton) findComponentById(ResourceTable.Id_btn_rotate);
        sBtnCrop = (SuperButton) findComponentById(ResourceTable.Id_btn_crop);
        mCropImageView = (CropImageView) findComponentById(ResourceTable.Id_crop_image_view);
        findComponentById(ResourceTable.Id_image_back).setClickedListener(this);
        sBtnSelect.setClickedListener(this);
        sBtnRotate.setClickedListener(this);
        sBtnCrop.setClickedListener(this);
        if (mCropImageView != null) {
            // 触摸时显示网格
            mCropImageView.setGuidelines(CropImageType.CROPIMAGE_GRID_ON);
            // 自由剪切
            // mCropImageView.setFixedAspectRatio(false);
            // 固定比例剪切
            mCropImageView.setFixedAspectRatio(true);
            mCropImageView.setAspectRatio(40, 30);
        }
        setBtnState(false);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                terminate();
                break;
            case ResourceTable.Id_btn_select:
                selectPic();
                break;
            case ResourceTable.Id_btn_rotate:
                mCropImageView.rotateImage(90);
                break;
            case ResourceTable.Id_btn_crop:
                mCropImageView.cropImage();
                //使用getCroppedImage获取裁剪的图片
                setBtnState(false);
                break;
        }
    }

    private void setBtnState(boolean enable) {
        if (enable) {
            sBtnRotate.setEnabled(true);
            sBtnCrop.setEnabled(true);
            ohos.agp.utils.Color color = new ohos.agp.utils.Color(ohos.agp.utils.Color.getIntColor("#299EE3"));
            sBtnRotate.setDisabledColor(null);
            sBtnRotate.invalidate();
            sBtnCrop.setDisabledColor(RgbColor.fromArgbInt(color.getValue()));
            sBtnRotate.invalidate();
        } else {
            sBtnRotate.setEnabled(false);
            sBtnCrop.setEnabled(false);
            ohos.agp.utils.Color color = new ohos.agp.utils.Color(0x20000000);
            sBtnRotate.setDisabledColor(RgbColor.fromArgbInt(color.getValue()));
            sBtnRotate.invalidate();
            sBtnCrop.setDisabledColor(RgbColor.fromArgbInt(color.getValue()));
            sBtnCrop.invalidate();
        }
    }

    private void selectPic() {
        if (verifySelfPermission("ohos.permission.READ_USER_STORAGE") != IBundleManager.PERMISSION_GRANTED
            || verifySelfPermission("ohos.permission.WRITE_USER_STORAGE") != IBundleManager.PERMISSION_GRANTED) {
            // 应用未被授予权限
            if (canRequestPermission("ohos.permission.READ_USER_STORAGE") || canRequestPermission("ohos.permission.WRITE_USER_STORAGE")) {
                // 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
                requestPermissionsFromUser(
                    storePermissions, MY_PERMISSIONS_REQUEST_STORE);
            }
        } else {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.GET_CONTENT");
            intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
            intent.setType("image/*");
            startAbilityForResult(intent, REQUEST_IMAGE);
        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        //选择系统图片并解析
        if (requestCode == REQUEST_IMAGE) {
            if (data != null) {
                Uri uri = data.getUri();
                if (uri != null) {
                    mCropImageView.setImagePath(CropDrawableUtils.displayPic(this, uri));
                    setBtnState(true);
                }
            }
        }
    }
}
